/**
 * 项目设置 列表页
 *
 * 作者 admin
 * 日期 2024-07-29 14:19:15
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'formSelects', 'table', 'validate','commonJS'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        commonJS = layui.commonJS,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        validate = layui.validate,
        $view = $('#febs-incentiveRewardMatters'),
        $query = $view.find('#query'),
        $add = $view.find('#add'),
        $searchForm = $view.find('#incentiveRewardMatters-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        $status = $searchForm.find('select[name="status"]'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'incentiveRewardMattersTable',
            url: ctx + 'incentiveRewardMatters/list',
            height: 'full-110',
            where: getQueryParams(),
            skin: '',
            cols: [[
                {title: '序号', type: 'numbers',width: 80},
                {field: 'mattersName', title: '奖励事项名称', minWidth: 260,hide:!hasHeadCompanyRole},
                {field: 'rewardAmount',align:'center', title: '奖励金额(元)', minWidth: 90},
                {field: 'rewardScore',align:'center', title: '奖励积分', minWidth: 90},
                {field: 'status', title: '是否启用', width: 100,toolbar: '#status-bar'},
                {field: 'sort',align:'center', title: '排序', width: 80},
                {field: 'createdBy',align:'center', title: '创建人', width: 120},
                {field: 'createdTime',align:'center', title: '创建时间', width: 180},
                {title: '操作',align:'center',  toolbar: '#incentiveRewardMatters-option', width: 100}
            ]],
            toolbar: '#incentiveRewardMatters-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(incentiveRewardMattersTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    table.on('tool(incentiveRewardMattersTable)', function (obj) {
        var data = obj.data,
            layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除事项', '确定删除该事项？', function () {
                deleteIds(data.rewardMattersId);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('修改奖励事项', 'incentiveRewardMatters/update/' + data.rewardMattersId, {
                area: $(window).width() <= 750 ? '90%': ['700px','420px'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#incentiveRewardMatters-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: $tenantSelect.val(),
                },
            });
        }
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(incentiveRewardMattersTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增奖励事项', 'incentiveRewardMatters/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? '90%': ['700px','420px'],
                yes: function (index, layero) {
                    $('#incentiveRewardMatters-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: $tenantSelect.val(),
                }
            });
        }
    });

    function getQueryParams() {
        return {
            tenantId: $tenantSelect.val(),
            mattersName: $searchForm.find('input[name="mattersName"]').val()?.trim(),
            status: $status.val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'incentiveRewardMatters/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }


    $(window).resize(function () {

    });

    form.on('switch(mattersStatus)', function (obj) {
        //区域状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其 postId
        var id =  table.cache['incentiveRewardMattersTable'][index].rewardMattersId;
        console.log("事项Id"+id);
        febs.modal.confirm('更改事项启用状态', '确认更改事项启用状态',function (){
            updateStatus(id,status);
        },function (){
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });
    function updateStatus(id,status) {
        //更新启用状态
        let model={
            rewardMattersId : id,
            status : status
        }
        febs.post(ctx + 'incentiveRewardMatters/updateStatus', model, function () {
            febs.alert.success('修改启用状态成功');
            reloadPage(1);
        })
    }
});